package com.taxi.gateway.exception;

import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taxi.common.dto.ResponseResult;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author: Ruomeng
 * @Date: 2024/12/12
 * @Description : online-taxi
 * @Version: 1.0
 */
public class OnlineTaxiExceptionHandler implements WebExceptionHandler {

    /**
     * 异常时处理方法
     *
     * @param exchange
     * @param ex
     * @return
     */
    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
        ResponseResult responseResult = null;
        if (ex instanceof FlowException) {
            responseResult = ResponseResult.fail(429, "自定义流控异常");
        }
        return writeResponse(exchange, responseResult);
    }

    /**
     * 编写返回值
     *
     * @param exchange
     * @param responseResult
     * @return
     */
    private Mono<Void> writeResponse(ServerWebExchange exchange, ResponseResult responseResult) {
        ServerHttpResponse response = exchange.getResponse();
        response.getHeaders().add("Content-Type", "application/json;charset=utf-8");
        String resultString = "";
        try {
            resultString = new ObjectMapper().writeValueAsString(responseResult);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        DataBuffer wrap = response.bufferFactory().wrap(resultString.getBytes());
        return response.writeWith(Flux.just(wrap));
    }

}
